package org.example.controller;

import org.example.domain.*;
import org.example.service.UserService;
import org.example.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author Jimmy
 * @version 1.0
 */
//@CrossOrigin(origins = "*",maxAge = 3600)
@Transactional(rollbackFor = RuntimeException.class)
@Controller
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 登录
     * @param user user
     * @return json
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public JsonData<User> login(@RequestBody User user, HttpServletRequest request){
        User retUser = userService.login(user.getEmail(),user.getPassword());
        JsonData<User> jsonData = new JsonData<>();
        Meta meta = new Meta();

        if (retUser != null){
            meta.setMsg("登录成功");
            meta.setStatus(200);
            String token = JwtUtils.generateToken(retUser.getId());
            retUser.setToken(token);
        }else {
            meta.setMsg("无此用户");
            meta.setStatus(404);
        }
        jsonData.setData(retUser);
        jsonData.setMeta(meta);
        return jsonData;
    }



    /**
     * 分页查询
     * @param pagenum 当前页码
     * @param pagesize 每页条目数
     * @return json
     */
   /* @RequestMapping(value = "/users",method = RequestMethod.GET)
    @ResponseBody
    public JsonData<PageBean> pageBean(int pagenum, int pagesize){
        JsonData<PageBean> jsonData = new JsonData<>();
        Meta meta = new Meta();
        PageBean pageBean = userService.findByPage(pagenum, pagesize);
        if (pageBean != null){
            meta.setMsg("获取成功");
            meta.setStatus(200);
        }else {
            meta.setMsg("获取失败");
            meta.setStatus(404);
        }
        jsonData.setData(pageBean);
        jsonData.setMeta(meta);
        return jsonData;
    }*/

    /**
     * 带条件的分页查询
     * @param username 用户名
     * @param email 邮箱
     * @param telephone 电话号码
     * @param pagenum 当前页码
     * @param pagesize 每页最大条目数
     * @return
     */
    @RequestMapping(value = "/users",method = RequestMethod.GET)
    @ResponseBody
    public JsonData<PageBean> pageBean(@RequestParam(required = false) String username,@RequestParam(required = false) String email,@RequestParam(required = false) String telephone,int pagenum, int pagesize){

        Meta meta ;

        PageBean pageBean = userService.findByPage(new User(email,username,telephone),pagenum, pagesize);
        if (pageBean != null){
            meta = new Meta("获取成功",200);
        }else {
            meta = new Meta("获取失败",404);
        }

        return new JsonData<>(pageBean,meta);
    }

    /**
     * 根据id返回一个user
     * @param id id
     * @return json
     */
    @RequestMapping(value = "/getUser/{id}",method = RequestMethod.GET)
    @ResponseBody
    public JsonData<User> findOne(@PathVariable(value = "id") String id){
        JsonData<User> jsonData = new JsonData<>();
        Meta meta;
        User findUser = userService.findById(id);
        if (findUser != null){
            meta = new Meta("获取成功",200);
        }else {
            meta = new Meta("无此用户",404);
        }
        jsonData.setData(findUser);
        return jsonData;
    }


    /**
     * 修改用户
     * @param id 用户id
     * @param user 用户
     * @return json
     */
    @RequestMapping(value = "/updateUser/{id}",method = RequestMethod.PUT)
    @ResponseBody
    public JsonData<String> updateUser(@PathVariable(value = "id") String id,@RequestBody(required = false) User user){
        JsonData<String> jsonData = new JsonData<>();
        Meta meta = new Meta();
        user.setId(id);
        int i = userService.update(user);
        if (i != 0){
            meta.setMsg("修改成功");
            meta.setStatus(200);
        }else {
            meta.setMsg("修改失败");
            meta.setStatus(404);
        }
        jsonData.setMeta(meta);
        return jsonData;
    }

    /**
     * 删除用户
     * @param id 用户id
     * @return json
     */
    @RequestMapping(value = "/delUser/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonData<String> delete(@PathVariable(value = "id")String id){
        JsonData<String> jsonData = new JsonData<>();
        Meta meta = new Meta();
        int i = userService.delete(id);
        if (i != 0){
            meta.setMsg("删除成功");
            meta.setStatus(200);
        }
        jsonData.setMeta(meta);
        return jsonData;
    }

    /**
     * 批量删除
     * @param ids 需要删除的用户id数组
     * @return json
     */
    @RequestMapping(value = "/delAllUser",method = RequestMethod.DELETE)
    @ResponseBody
    public JsonData<String> deleteAll(@RequestBody Map<String, List<String>> ids){
        for (String id : ids.get("ids")) {
            int i = userService.delete(id);
        }
        Meta meta = new Meta("删除成功",200);

        return new JsonData<>(meta);
    }


    /**
     * 用户菜单权限
     * @param request req
     * @return json
     */
    @RequestMapping(value = "/menus",method = RequestMethod.GET)
    @ResponseBody
    public JsonData<List<Model>> menus(HttpServletRequest request){
        JsonData<List<Model>> jsonData = new JsonData<>();
        Meta meta = new Meta();
        String token = request.getHeader("token");
        String id = JwtUtils.getUserIdFromToken(token);
        List<String> rids = userService.findRidById(id);

        if (rids != null && rids.size()>0){
            List<Model> models = userService.userPower(rids);
            jsonData.setData(models);
            meta.setMsg("获取菜单列表成功");
            meta.setStatus(200);
        }else{
            meta.setMsg("该用户无权限");
            meta.setStatus(401);
        }

        jsonData.setMeta(meta);
        return jsonData;
    }

}
